package dimension.starry.item;

import dimension.starry.item.food.Shit;
import net.minecraft.entity.ItemEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.screen.ScreenHandler;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.world.World;

import static dimension.starry.constants.ItemNbtTag.FOOD_CREATED_TIME;

/**
 * @author 150149
 */
public class FoodRot {

    public static boolean onPlayerOpenContainer(ServerPlayerEntity player, ScreenHandler screenHandler) {
        screenHandler.slots.stream()
            .filter(slot -> !(slot.getStack().getItem() instanceof Shit))
            .filter(slot -> slot.getStack().getItem().getFoodComponent() != null)
            .forEach(slot -> slot.setStack(addFoodCreatedTime(slot.getStack(), player.getServerWorld())));
        for (int i = 0; i < player.getInventory().size(); i++) {
            ItemStack itemStack = player.getInventory().getStack(i);
            if (itemStack.getItem() instanceof Shit || itemStack.getItem().getFoodComponent() == null) {
                continue;
            }
            player.getInventory().setStack(i, addFoodCreatedTime(itemStack, player.getServerWorld()));
        }
        return true;
    }

    private static ItemStack addFoodCreatedTime(ItemStack itemStack, World world) {
        NbtCompound nbtCompound = itemStack.getOrCreateNbt();
        if (nbtCompound.contains(FOOD_CREATED_TIME)) {
            long foodTime = nbtCompound.getLong(FOOD_CREATED_TIME);
            if (world.getTime() - foodTime > 100000) {
                return new ItemStack(ItemRegisterManager.SHIT.getItem());
            }
        } else {
            long foodTime = world.getTime();
            nbtCompound.putLong(FOOD_CREATED_TIME, foodTime);
        }
        itemStack.setNbt(nbtCompound);
        return itemStack;
    }

    public static void onPlayerPickupItem(PlayerEntity player, ItemEntity itemEntity) {
        if (player instanceof ServerPlayerEntity serverPlayer) {
            if (itemEntity.getStack().getItem() instanceof Shit) {
                return;
            }
            ItemStack itemStack = itemEntity.getStack();
            if (itemStack.getItem().getFoodComponent() != null) {
                NbtCompound nbtCompound = itemStack.getOrCreateNbt();
                if (nbtCompound.contains(FOOD_CREATED_TIME)) {
                    long foodTime = nbtCompound.getLong(FOOD_CREATED_TIME);
                    if (serverPlayer.getServerWorld().getTime() - foodTime > 100000) {
                        itemEntity.setStack(new ItemStack(ItemRegisterManager.SHIT.getItem()));
                    }
                } else {
                    long foodTime = serverPlayer.getServerWorld().getTime();
                    nbtCompound.putLong(FOOD_CREATED_TIME, foodTime);
                }

                for (ItemStack item : serverPlayer.getInventory().main) {
                    NbtCompound nbtCompound1 = item.getNbt();
                    if (nbtCompound1 == null) {
                        continue;
                    }
                    if (nbtCompound1.contains(FOOD_CREATED_TIME) &&
                        itemStack.getItem().getTranslationKey().equals(item.getItem().getTranslationKey())) {
                        long foodTime2 = nbtCompound1.getLong(FOOD_CREATED_TIME);
                        long foodTime1 = nbtCompound.getLong(FOOD_CREATED_TIME);
                        if (Math.abs(foodTime1 - foodTime2) < 2000) {
                            nbtCompound.putLong(FOOD_CREATED_TIME, (foodTime1 + foodTime2) / 2);
                            nbtCompound1.putLong(FOOD_CREATED_TIME, (foodTime1 + foodTime2) / 2);
                            item.setNbt(nbtCompound1);
                        }
                    }
                }
                itemStack.setNbt(nbtCompound);
                itemEntity.setStack(itemStack);
            }
        }
    }

}
